package com.cwd.arm.application.service.read;

import com.cwd.arm.infrastructure.dao.PayDao;
import com.cwd.arm.infrastructure.po.PayPo;
import com.cwd.arm.application.dto.PayDto;
import com.cwd.arm.application.service.read.facade.PayReadService;
import com.cwd.arm.infrastructure.query.PayDaoQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
* Description:现金支出
* Created on 2021年11月20日
* @author  yan
*/
@Repository
@Primary
public class PayReadServiceImpl implements PayReadService {

    @Autowired
    private PayDao payDao;

    @Override
    public PayDto selectById(Long id) {
        return convertToDto(payDao.getById(id));
    }

    @Override
    public List<PayDto> selectByProjectId(Long projectId) {
        PayDaoQuery daoQuery = new PayDaoQuery();
        daoQuery.setProjectId(projectId);
        List<PayPo> poList = payDao.selectByQuery(daoQuery);
        return convertToDtoList(poList);
    }

    private List<PayDto> convertToDtoList(List<PayPo> poList){
        return poList.stream().map(po->convertToDto(po)).collect(Collectors.toList());
    }

    private PayDto convertToDto(PayPo po){
        PayDto dto = new PayDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

}
